import cv2
import numpy as np
from PIL import Image


#
# filename = '1.png'
#
# # cv2转PIL
# img = cv2.imread(filename, cv2.IMREAD_UNCHANGED)
# img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGRA2RGBA))
# img.show()
#
# # PIL转cv2
# img = Image.open(filename)
# img = cv2.cvtColor(np.array(img), cv2.COLOR_RGBA2BGRA)
# cv2.imshow(filename, img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# 参照 https://blog.csdn.net/lly1122334/article/details/108093692

def img_cv2_to_pil(im):
    try:
        return  Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGRA2RGBA))

    except:

        return Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))



def img_pil_to_cv2(im):
    try:
        return cv2.cvtColor(np.array(im), cv2.COLOR_RGBA2BGRA)
    except:
        return cv2.cvtColor(np.array(im), cv2.COLOR_RGB2BGR)

